package com.bright.hxj.qchz.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bright.hxj.qchz.mapper.DuijieJieshouUserMapper;
import com.bright.hxj.qchz.pojo.po.Duijie_ShiJieShouUser;
import com.bright.hxj.qchz.service.Duijie_ShiJieShouUserManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
@DS("datasource-shiJieShou")
public class Duijie_ShiJieShouUserManagerImpl extends ServiceImpl<DuijieJieshouUserMapper, Duijie_ShiJieShouUser> implements Duijie_ShiJieShouUserManager {


    @Override
    public Duijie_ShiJieShouUser getUser(String userName) {

//        String sql = "SELECT id, userName, password, distNo, distName, visible, head, qu_url, tongji_url, acc_set_add FROM Duijie_JieShou_User WHERE userName=?";
//        List<List<Object>> data = duijie_shiJieShou_jdbcTemplate.queryForListList(sql, new Object[]{userName});

        Duijie_ShiJieShouUser user = getOne(new LambdaQueryWrapper<Duijie_ShiJieShouUser>()
                .eq(Duijie_ShiJieShouUser::getUserName, userName)
                .eq(Duijie_ShiJieShouUser::getVisible, true), false);

        if (user != null) {
            if (StringUtils.isEmpty(user.getQu_url()) || StringUtils.isEmpty(user.getTongji_url())) {
                throw new RuntimeException(user.getUserName() + " 的qu_url和tongji_url不能为空！");
            }

            if (!StringUtils.right(user.getQu_url(), 1).equals("/")) {
                user.setQu_url(user.getQu_url() + "/");
            }
        }

        return user;
    }


    public Duijie_ShiJieShouUser getUser_byDistNo(String distNo) {
//        Duijie_ShiJieShouUser rvalue = null;

//        String sql = "SELECT userName FROM Duijie_JieShou_User WHERE distNo=?";
//        List<List<Object>> data = duijie_shiJieShou_jdbcTemplate.queryForListList(sql, new Object[]{distNo});
//        List<List<Object>> data = new ArrayList<>();
//        if(data.size() > 0) {
//            rvalue = getUser((String) data.get(0).get(0));
//        }
        Duijie_ShiJieShouUser user = getOne(new LambdaQueryWrapper<Duijie_ShiJieShouUser>()
                .eq(Duijie_ShiJieShouUser::getDistNo, distNo)
                .eq(Duijie_ShiJieShouUser::getVisible, true), false);

        return user;
    }
}
